package com.zhia.demo.controller;

import com.zhia.demo.entity.ReviewEntity;
import com.zhia.demo.entity.UserEntity;
import com.zhia.demo.service.RecoverService;
import com.zhia.demo.service.ReviewService;
import com.zhia.demo.vo.ResultVo;
import com.zhia.demo.vo.ReviewVo;
import com.zhia.demo.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;


/**
 * @author DONG
 * @create 2023/11/4 15:57
 */
@RestController
public class ReviewController {
    @Autowired
    private ReviewService reviewService;

    @PostMapping("/review/save")
    public void save(ReviewEntity reviewEntity, @CookieValue(name = "USERSESSION", defaultValue = "") String cookie) {
        if (!StringUtils.isEmpty(cookie)) {
            reviewEntity.setUserId(Integer.parseInt(cookie));
            reviewEntity.setGiveLike(0L);
            reviewEntity.setCreateTime(new Timestamp(new Date().getTime()));
            reviewService.save(reviewEntity);
        }

    }

    @GetMapping("/review/{bookId}")
    public ResultVo reviewList(@PathVariable("bookId") String bookId, @CookieValue(name = "USERSESSION", defaultValue = "") String cookie) {
        List<ReviewVo> reviewVos;
        if (!StringUtils.isEmpty(cookie)) {
            reviewVos = reviewService.getReviewList(bookId, Integer.parseInt(cookie));
        } else {
            reviewVos = reviewService.getReviewList(bookId, null);
        }
        ResultVo resultVo = new ResultVo();
        resultVo.setData(reviewVos);
        return resultVo;
    }

    @DeleteMapping("/review/{id}")
    public void delete(@PathVariable("id") Integer id) {
        reviewService.delete(id);
    }
}
